# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=lua5.3
_pkgname=lua
pkgver=5.3.6
_luaver=${pkgname#lua}
pkgrel=6
pkgdesc="Powerful light-weight programming language"
url="https://www.lua.org/"
arch="all"
license="MIT"
ldpath="/usr/lib/$pkgname"
depends_dev="$pkgname"
makedepends="libtool autoconf automake linenoise-dev"
provides="lua"
provider_priority=300
subpackages="$pkgname-dbg $pkgname-dev $pkgname-doc $pkgname-libs"
source="https://www.lua.org/ftp/$_pkgname-$pkgver.tar.gz
	lua-5.3-make.patch
	lua-5.3-module_paths.patch
	linenoise.patch
	"
builddir="$srcdir/$_pkgname-$pkgver"

# secfixes:
#   5.3.5-r2:
#     - CVE-2019-6706

prepare() {
	default_prepare
	# disable readline
	sed -i '/#define LUA_USE_READLINE/d' src/luaconf.h

	# we use libtool
	cat > configure.ac <<-EOF
		top_buildir=.

		AC_INIT(src/luaconf.h)
		AC_PROG_LIBTOOL
		AC_OUTPUT()
	EOF
	libtoolize --force --install && aclocal && autoconf
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr

	cd src
	make V=${pkgver%.*} \
		CFLAGS="$CFLAGS -DLUA_USE_LINUX -DLUA_COMPAT_5_2 -DLUA_USE_LINENOISE" \
		SYSLDFLAGS="$LDFLAGS" \
		RPATH="/usr/lib" \
		LIB_LIBS="-lpthread -lm -ldl -llinenoise" \
		alpine_all
}

check() {
	make test
}

package() {
	local i
	make V=${pkgver%.*} \
		INSTALL_TOP="$pkgdir"/usr \
		INSTALL_INC="$pkgdir"/usr/include/$pkgname \
		INSTALL_LIB="$pkgdir"/usr/lib/$pkgname \
		alpine_install

	for i in "$pkgdir"/usr/bin/* ; do
		mv "$i" "$i"$_luaver
	done

	for i in "$pkgdir"/usr/lib/$pkgname/*.so.*; do
		ln -s $pkgname/${i##*/} "$pkgdir"/usr/lib/${i##*/}
	done

	install -D -m 644 doc/lua.1 "$pkgdir"/usr/share/man/man1/lua$_luaver.1
	install -D -m 644 doc/luac.1 "$pkgdir"/usr/share/man/man1/luac$_luaver.1

	mkdir -p "$pkgdir"/usr/share/doc/$pkgname/
	install -m644 doc/*.html doc/*.css doc/logo.gif doc/osi-certified-72x60.png \
		"$pkgdir"/usr/share/doc/$pkgname/

	install -d "$pkgdir"/usr/lib/pkgconfig
	cat > "$pkgdir"/usr/lib/pkgconfig/lua$_luaver.pc <<-EOF
		# lua.pc -- pkg-config data for Lua

		# vars from install Makefile

		# grep '^V=' ../Makefile
		V=$_luaver
		# grep '^R=' ../Makefile
		R=$pkgver

		# grep '^INSTALL_.*=' ../Makefile | sed 's/INSTALL_TOP/prefix/'
		prefix=/usr
		INSTALL_BIN=\${prefix}/bin
		INSTALL_INC=\${prefix}/include
		INSTALL_LIB=\${prefix}/lib
		INSTALL_MAN=\${prefix}/man/man1
		INSTALL_LMOD=\${prefix}/share/lua/\${V}
		INSTALL_CMOD=\${prefix}/lib/lua/\${V}

		# canonical vars
		exec_prefix=\${prefix}
		libdir=\${exec_prefix}/lib/$pkgname
		includedir=\${prefix}/include/$pkgname

		Name: Lua
		Description: An Extensible Extension Language
		Version: ${pkgver}
		Requires:
		Libs: -L\${libdir} -llua -lm
		Cflags: -I\${includedir}

		# (end of lua$_luaver.pc)
	EOF

	# compat names since we are unique
	# other distros name it lua-5.3 or lua53. pick the former as a compat name..
	ln -sv lua5.3.pc "$pkgdir"/usr/lib/pkgconfig/lua-5.3.pc
}

dev() {
	default_dev

	mkdir -p "$subpkgdir"/usr/$pkgname "$subpkgdir"/usr/lib/$pkgname
	ln -s ../include/$pkgname "$subpkgdir"/usr/$pkgname/include
	ln -s ../lib/$pkgname "$subpkgdir"/usr/$pkgname/lib
	mv "$pkgdir"/usr/lib/$pkgname/liblua.so "$subpkgdir"/usr/lib/$pkgname/
}

libs() {
	pkgdesc="Lua dynamic library runtime"
	replaces="lua"

	mkdir -p "$subpkgdir"/usr
	mv "$pkgdir"/usr/lib "$subpkgdir"/usr/
}

sha512sums="
ccc380d5e114d54504de0bfb0321ca25ec325d6ff1bfee44b11870b660762d1a9bf120490c027a0088128b58bb6b5271bbc648400cab84d2dc22b512c4841681  lua-5.3.6.tar.gz
1bc6c623024c1738155b30ff9c0edcce0f336edc25aa20c3a1400c859421ea2015d75175cce8d515e055ac3e96028426b74812e04022af18a0ed4c4601556027  lua-5.3-make.patch
bc68772390dc8d8940176af0b9fbacc0af61891b5d27de5f1466a4e7f9b3291a1c08ba5add829bc96b789a53fa5ec2dadaa096ca6eabe54ec27724fa2810940f  lua-5.3-module_paths.patch
a2edcf5a41513492edff5fa6e97652e676ceb6c66bb2c6a7e6f345570248d7646167f0172ceb07a74fd9d4a43051ed8244fbd94706dd9f5593f174075592a527  linenoise.patch
"
